#include "stdafx.h"
#include "Utility.h"

// Callback function for director selecting functions
int CALLBACK BrowseCallbackProc(HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData)
{
	// If the BFFM_INITIALIZED message is received
	// set the path to the start path.
	switch (uMsg)
	{
		case BFFM_INITIALIZED:
		{
			if (NULL != lpData)
			{
				SendMessage(hwnd, BFFM_SETSELECTION, TRUE, lpData);
			}
		}
		break;

		//case BFFM_SELCHANGED: 
		//{
		//	if (NULL != lpData)
		//	{
		//		SendMessage(hwnd, BFFM_SETSTATUSTEXT, TRUE, lpData);
		//	}
		//}
		//break;
	}

	return 0; // The function should always return 0.
}

// Recursively delete directory's contents
void RecursiveDelete(const CString& szPath)
{
	CFileFind ff;
	CString path = szPath;

	if (path.Right(1) != _T("\\")) {
		path += _T("\\");
	}

	path += _T("*.*");

	BOOL res = ff.FindFile(path);

	while (res) {
		res = ff.FindNextFileW();

		if (!ff.IsDots() && !ff.IsDirectory()) {
			DeleteFile(ff.GetFilePath());
		} else if (ff.IsDirectory() && !ff.IsDots()) {
			path = ff.GetFilePath();
			RecursiveDelete(path);
            ::RemoveDirectoryW(path);
		}
	}
}

// Delete the sub-directory's content and subdirectory itself
void DeleteSubDirectory(const CString& szPath)
{
	CFileFind ff;
	CString path = szPath;

	if (path.Right(1) != _T("\\")) {
		path += _T("\\");
	}

	path += _T("*.*");

	BOOL res = ff.FindFile(path);
	while (res) {
		res = ff.FindNextFileW();
        
        // Dots directory such as . and  ..
		if (ff.IsDots())
			continue;

		if (ff.IsDirectory()) {
			RecursiveDelete(ff.GetFilePath());
            ::RemoveDirectoryW(ff.GetFilePath());
		}
	}

}